/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.cx1.model.Blog;
import org.cx1.model.ds.MockDS;
import org.cx1.model.utils.BlogComparator;
import org.cx1.model.utils.ServiceResponse;
import org.cx1.service.api.IBlogService;
import org.cx1.service.utils.IDGenerator;

/**
 *
 * @author ECH
 */
public class MBlogService implements IBlogService {

    @Override
    public ServiceResponse<String> create(Blog blog) {
        ServiceResponse<String> response = new ServiceResponse<String>();
        String id = null;        
        try {            
            Blog entry = new Blog();
            id = IDGenerator.generateID();
            
            entry.setBlogTypeID(blog.getBlogTypeID());
            
            entry.setId(id);
            entry.setTitle(blog.getTitle());
            entry.setBody(blog.getBody());
            entry.setShortBody(blog.getShortBody());
            
            entry.setAuthorName(blog.getAuthorName());
            entry.setAuthorGPlus(blog.getAuthorGPlus());
            
            entry.setTagIDs(blog.getTagIDs());
            entry.setPlaceIDs(blog.getPlaceIDs());
            
            entry.setActive(true);
            entry.setDateCreated(new Date());
            entry.setDateModified(new Date());
                        
            MockDS.getBlogs().add(entry);
            
            response.setResponse(id);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse("");
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<Blog> read(String id) {
        ServiceResponse<Blog> response = new ServiceResponse<Blog>();
        Blog blog = null;
        try {            
            List<Blog> records = MockDS.getBlogs();
            if (records != null && records.size() > 0) {
                for (Blog record : records) {
                    if (StringUtils.equalsIgnoreCase(record.getId(), id)) {
                        blog = record;
                        break;
                    }
                }
            }                        
            response.setResponse(blog);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<List<Blog>> readAll(int offset, int limit) {
        ServiceResponse<List<Blog>> response = new ServiceResponse<List<Blog>>();
        List<Blog> blogs = new ArrayList<Blog>();
        try {            
            List<Blog> records = MockDS.getBlogs();
            if (records != null && records.size() > 0) {
                if (offset == 0 && limit == 0) {
                    blogs.addAll(records);
                }
                else if (offset < records.size() && limit < records.size()) {
                    blogs.addAll( records.subList(offset, limit) );
                }              
            }            
            
            buildBlogAttributes(blogs);
            Collections.sort(blogs, new BlogComparator());
            
            response.setResponse(blogs);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);    
        }
        return response; 
    }

    private void buildBlogAttributes(List<Blog> blogs) {
        if (blogs != null && blogs.size() > 0) {
            for (Blog blog : blogs) {
                if (!StringUtils.isEmpty(blog.getTitle())) {
                    blog.setUrlTitle( blog.getTitle().trim().toLowerCase().replaceAll(" ", "-") );                    
                }
            }
        }
    }
    
    @Override
    public ServiceResponse<Boolean> update(Blog blog) {
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {            
            Blog targetBlog = null;
            String targetID = blog.getId();            
            List<Blog> records = MockDS.getBlogs();
            
            if ( !StringUtils.isEmpty(targetID) && records != null && records.size() > 0) {                
                //CX: find record
                for (Blog record : records) {
                    if (StringUtils.equalsIgnoreCase(record.getId(), targetID)) {
                        targetBlog = record;
                        break;
                    }
                }                
                //CX: update dabase record
                if (targetBlog != null) {                 
                    targetBlog.setBlogTypeID(blog.getBlogTypeID());
                    
                    targetBlog.setTitle(blog.getTitle());
                    targetBlog.setBody(blog.getBody());
                    targetBlog.setShortBody(blog.getShortBody());
                    
                    targetBlog.setAuthorName(blog.getAuthorName());
                    targetBlog.setAuthorGPlus(blog.getAuthorGPlus());
                    targetBlog.setTagIDs(blog.getTagIDs());
                    targetBlog.setPlaceIDs(blog.getPlaceIDs());

                    targetBlog.setDateModified(new Date());            
                    success = true;
                }                
            }                
            response.setResponse(success);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);  
        }
        return response; 
    }

    @Override
    public ServiceResponse<Boolean> delete(String id) {
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {            
            List<Blog> records = MockDS.getBlogs();
            if (records != null && records.size() > 0) {
                
                for (int i = 0; i < records.size(); i++) {
                    Blog record = records.get(i);
                    
                    if (StringUtils.equalsIgnoreCase(record.getId(), id)) {
                        records.remove(i);
                        success = true;
                        break;
                    }
                }
            }            
            response.setResponse(success);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<List<Blog>> readAllByType(int offset, int limit, String blogTypeID) {
        ServiceResponse<List<Blog>> response = new ServiceResponse<List<Blog>>();
        List<Blog> blogs = new ArrayList<Blog>();
        List<Blog> typeBlogs = new ArrayList<Blog>();
        
        try {            
            List<Blog> records = MockDS.getBlogs();            
            if (records != null && records.size() > 0) {
                for (Blog blog : records) {
                    if (StringUtils.equalsIgnoreCase(blog.getBlogTypeID(), blogTypeID)) {
                        typeBlogs.add(blog);
                    }
                }
            }
            
            if (typeBlogs != null && typeBlogs.size() > 0) {
                if (offset == 0 && limit == 0) {
                    blogs.addAll(typeBlogs);
                }
                else if (offset < typeBlogs.size() && limit < typeBlogs.size()) {
                    blogs.addAll( typeBlogs.subList(offset, limit) );
                }              
            }            
            response.setResponse(blogs);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);    
        }
        return response; 
    }
        
}
